//
//  FirstScreenVC.swift
//  iot
//
//  Created by xinliang.yuan on 2024/4/16.
//

import UIKit
import SnapKit

class FirstScreenVC: UIViewController, UITextViewDelegate {
    
    var clickRange1 = NSRange(location: 0, length: 0)
    var clickRange2 = NSRange(location: 0, length: 0)
    var clickRange3 = NSRange(location: 0, length: 0)
    var clickRange4 = NSRange(location: 0, length: 0)
    var clickRange5 = NSRange(location: 0, length: 0)
    
    var protocolTV: UITextView = UITextView()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.isNavigationBarHidden = true
        view.backgroundColor = .white
        initUI()
    }
    
    func initUI() {
        
        let logo = UIImageView(image: UIImage(named: "house"))
        view.addSubview(logo)
        logo.snp.makeConstraints { make in
            make.width.height.equalTo(150)
            make.centerX.equalToSuperview()
            make.top.equalTo(150)
        }
        
        let passwordLoginBtn = UIButton()
        passwordLoginBtn.setTitle("密码登陆", for: .normal)
        passwordLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        passwordLoginBtn.setTitleColor(.black, for: .normal)
        view.addSubview(passwordLoginBtn)
        passwordLoginBtn.snp.makeConstraints { make in
            make.top.equalTo(50)
            make.right.equalTo(-20)
            make.height.equalTo(44)
            make.width.greaterThanOrEqualTo(20)
        }
        
        let operationView = UIView()
//        operationView.backgroundColor = .cyan
        view.addSubview(operationView)
        operationView.snp.makeConstraints { make in
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.left.equalTo(30)
            make.right.equalTo(-30)
//            make.height.greaterThanOrEqualTo(300)
        }
        
        let phoneLabel = UILabel()
        phoneLabel.text = "159****8957"
        phoneLabel.font = UIFont.systemFont(ofSize: 22, weight: .medium)
        operationView.addSubview(phoneLabel)
        phoneLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        let phoneMsgLable = UILabel()
        phoneMsgLable.text = "中国移动提供认证服务"
        phoneMsgLable.font = UIFont.systemFont(ofSize: 11)
        phoneMsgLable.textColor = .gray
        operationView.addSubview(phoneMsgLable)
        phoneMsgLable.snp.makeConstraints { make in
            make.top.equalTo(phoneLabel.snp.bottom).offset(6)
            make.centerX.equalToSuperview()
        }
        
        let loginBtn = UIButton()
        loginBtn.setTitle("一键登录", for: .normal)
        loginBtn.layer.cornerRadius = 8
        loginBtn.setTitleColor(.white, for: .normal)
        loginBtn.backgroundColor = .blue.withAlphaComponent(0.5)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        operationView.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { make in
            make.top.equalTo(phoneMsgLable.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(45)
        }
        
        let experienceBtn = UIButton()
        experienceBtn.setTitle("立即体验", for: .normal)
        experienceBtn.layer.cornerRadius = 8
        experienceBtn.layer.borderWidth = 0.5
        experienceBtn.layer.borderColor = UIColor.lightGray.cgColor
        experienceBtn.setTitleColor(.black, for: .normal)
        experienceBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        operationView.addSubview(experienceBtn)
        experienceBtn.snp.makeConstraints { make in
            make.top.equalTo(loginBtn.snp.bottom).offset(12)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(45)
        }
        
        let wechatBtn = UIButton()
        wechatBtn.setImage(UIImage(named: "wechat"), for: .normal)
        wechatBtn.backgroundColor = .green.withAlphaComponent(0.5)
        wechatBtn.layer.cornerRadius = 24
        wechatBtn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        operationView.addSubview(wechatBtn)
        wechatBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(experienceBtn.snp.bottom).offset(40)
            make.width.height.equalTo(48)
        }
        
        let qqBtn = UIButton()
        qqBtn.setImage(UIImage(named: "qq"), for: .normal)
        qqBtn.backgroundColor = .blue
        qqBtn.layer.cornerRadius = 24
        qqBtn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        operationView.addSubview(qqBtn)
        qqBtn.snp.makeConstraints { make in
            make.right.equalTo(wechatBtn.snp.left).offset(-25)
            make.top.equalTo(experienceBtn.snp.bottom).offset(40)
            make.width.height.equalTo(48)
        }
        
        let appleBtn = UIButton()
        appleBtn.setImage(UIImage(named: "apple"), for: .normal)
        appleBtn.backgroundColor = .black
        appleBtn.layer.cornerRadius = 24
        appleBtn.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        operationView.addSubview(appleBtn)
        appleBtn.snp.makeConstraints { make in
            make.left.equalTo(wechatBtn.snp.right).offset(25)
            make.top.equalTo(experienceBtn.snp.bottom).offset(40)
            make.width.height.equalTo(48)
        }
        
        let linkView = UIView()
        operationView.addSubview(linkView)
        linkView.snp.makeConstraints { make in
            make.top.equalTo(qqBtn.snp.bottom).offset(20)
            make.left.right.equalTo(0)
            make.bottom.equalToSuperview()
        }
        
        let clickBtn = UIButton()
        clickBtn.setImage(UIImage(named: "click"), for: .normal)
        clickBtn.setImage(UIImage(named: "click_selected"), for: .selected)
        clickBtn.addTarget(self, action: #selector(clickAction(_:)), for: .touchUpInside)
        linkView.addSubview(clickBtn)
        clickBtn.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.width.height.equalTo(15)
        }
        
        let text = "我同意《中国移动认证服务条款》《隐私政策》《用户协议》和《儿童隐私保护声明》和《第三方信息共享和SDK服务清单》"
        protocolTV = UITextView()
        protocolTV.delegate = self
        protocolTV.isEditable = false
//        protocolTV.isSelectable = false
        protocolTV.isScrollEnabled = false
        protocolTV.textContainerInset = UIEdgeInsets.zero
        protocolTV.linkTextAttributes = [:]
        protocolTV.dataDetectorTypes = .link

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 5 // 行高为 8 点
        let attriTxt = NSMutableAttributedString(string: text, attributes: [.paragraphStyle: paragraphStyle])
        let r1 = NSRange(text.range(of: "《中国移动认证服务条款》")!, in: text)
        let r2 = NSRange(text.range(of: "《隐私政策》")!, in: text)
        let r3 = NSRange(text.range(of: "《用户协议》")!, in: text)
        let r4 = NSRange(text.range(of: "《儿童隐私保护声明》")!, in: text)
        let r5 = NSRange(text.range(of: "《第三方信息共享和SDK服务清单》")!, in: text)
        clickRange1 = r1
        clickRange2 = r2
        clickRange3 = r3
        clickRange4 = r4
        clickRange5 = r5
        attriTxt.addAttributes([
            .foregroundColor: UIColor.blue.withAlphaComponent(0.8),
            .link: "v1://"
        ], range: r1)
        attriTxt.addAttributes([
            .foregroundColor: UIColor.blue.withAlphaComponent(0.8),
            .link: "v2://"
        ], range: r2)
        attriTxt.addAttributes([
            .foregroundColor: UIColor.blue.withAlphaComponent(0.8),
            .link: "v3://"
        ], range: r3)
        attriTxt.addAttributes([
            .foregroundColor: UIColor.blue.withAlphaComponent(0.8),
            .link: "v4://"
        ], range: r4)
        attriTxt.addAttributes([
            .foregroundColor: UIColor.blue.withAlphaComponent(0.8),
            .link: "v5://"
        ], range: r5)
        protocolTV.attributedText = attriTxt
        protocolTV.font = UIFont.systemFont(ofSize: 12)
        linkView.addSubview(protocolTV)
        protocolTV.snp.makeConstraints { make in
            make.left.equalTo(clickBtn.snp.right).offset(15)
            make.top.right.bottom.equalToSuperview()
        }
    }
    
    @objc func clickAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    // UITextViewDelegate 方法，处理点击链接事件
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        print(URL.absoluteString)
        return false
    }
    
    // UITextViewDelegate 方法，处理点击链接事件, 兼容iOS17之后
    @available(iOS 17.0, *)
    func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
        var action: UIAction?
        if clickRange1 == textItem.range {
            action = UIAction { action in
                print("1")
            }
        } else if clickRange2 == textItem.range {
            action = UIAction { action in
                print("2")
            }
        } else if clickRange3 == textItem.range {
            action = UIAction { action in
                print("3")
            }
        } else if clickRange4 == textItem.range {
            action = UIAction { action in
                print("4")
            }
        } else if clickRange5 == textItem.range {
            action = UIAction { action in
                print("5")
            }
        }
        return action
    }
    
}
